import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class Sala extends GestorBD{
	private String nom;
	private String centre;
	private int metresQuadrats;
	private String desc;
	private Manager mngr=new Manager();
	
	@Override
	public void inserir() throws SQLException {
		this.connect();
		if (this.existeixBD()) {
			JOptionPane.showMessageDialog(new JFrame("Error"),
				    "Aquesta sala ja existeix.",
				    "Sala existent",
				    JOptionPane.ERROR_MESSAGE);
		} else {
			pstmt = connexio.prepareStatement("INSERT INTO sala(nom,centre,metresQuadrats,descripcio) VALUES (?,?,?,?)");
			pstmt.setString(1, this.getNom());
			pstmt.setString(2, this.getCentre());
			pstmt.setInt(3, this.getMetresQuadrats());
			pstmt.setString(4, this.getDesc());
			pstmt.executeUpdate();
		}
		this.closeConnection(connexio);
	}

	@Override
	public void eliminar() throws SQLException {
		this.connect();
		pstmt = connexio.prepareStatement("DELETE FROM sala WHERE nom='"+ this.nom+"' and centre='"+this.centre+"'");
		pstmt.executeUpdate();
		this.closeConnection(connexio);
	}

	public void modificar(String nouNom,String nouCentre,int nouMetresQuadrats,String nouDesc) throws SQLException {
		this.connect();
		if (this.existeixBD()) {
			pstmt = connexio.prepareStatement("UPDATE sala SET nom=?,centre=?,metresQuadrats=?,descripcio=? WHERE nom='"+ this.nom+"' and centre LIKE '"+this.centre+"'");
			pstmt.setString(1, nouNom);
			pstmt.setString(2, nouCentre);
			pstmt.setInt(3, nouMetresQuadrats);
			pstmt.setString(4, nouDesc);
			pstmt.executeUpdate();
		}
		this.closeConnection(connexio);
	}
	@Override
	public boolean existeixBD() throws SQLException {
		this.connect();
		stmt = connexio.createStatement();
		rs = stmt.executeQuery("SELECT COUNT(*) FROM sala WHERE nom LIKE '"+this.nom+"' and centre LIKE '"+this.centre+"'");
		rs.next();
		int comprovar = rs.getInt(1);
		if (comprovar == 1) {
			return true;
		} else {
			return false;
		}
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getCentre() {
		return centre;
	}

	public void setCentre(String centre) {
		this.centre = centre;
	}

	public int getMetresQuadrats() {
		return metresQuadrats;
	}

	public void setMetresQuadrats(int metresQuadrats) {
		this.metresQuadrats = metresQuadrats;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public Manager getMngr() {
		return mngr;
	}

	public void setMngr(Manager mngr) {
		this.mngr = mngr;
	}
	
	
	

}
